from abc import abstractmethod
from .InstructionSrc import InstructionSrc
from .MyLogger import MyLogger
from .UIInstruction import GetInstTable
from .UIInstruction import UIInstruction
from .Instruction import Instruction
from .PersistStorage import PersistStorage
from .UIException import *

class UserInterface:
    def __init__(self, instSrc:InstructionSrc, logger:MyLogger, cfg:PersistStorage) -> None:
        self.instSrc = instSrc
        self.logger = logger
        Instruction.SetLogger(logger)
        self.instTable = GetInstTable()
        self.cfg = cfg
        Instruction.SetCfg(cfg)

    def GetInstAndExecute(self) -> None:
        instTuple = self.instSrc.GetInstruction()
        instStr = instTuple[0]
        if instStr not in self.instTable:
            raise UIUnsupportedInstruction('Instruction not found')
        inst:UIInstruction = self.instTable[instStr]
        inst.Execute(instTuple[1:])

    @abstractmethod
    def MainLoop(self) -> None: ...